<?php
$aPriKey = array();
$aPriKey['N'] = _t('not primary-key');
$aPriKey['Y'] = _t('primary-key');
$aPriKey['X'] = _t('primary-key auto-increment');

/**
 * attribute insert
 */
function attributeIns() {
	global $objSDAO;
	$errMsg = "";
	$_REQUEST["attribute"] = strtolower(trim($_REQUEST["attribute"]));
	if(ereg("[^a-z0-9_]",sxREQUEST("attribute")))
		$errMsg .= _t("name : only a-z 0-9 _").".<br>";
	if(sxREQUEST("attribute") == "")
		$errMsg .= _t("missing name")."<br>";
		
	if(sxREQUEST("sequence") == "" )
		$_REQUEST["sequence"] = "0";
	if(ereg("[^0-9_]",sxREQUEST("sequence")))
		$errMsg .= _t("sequence not numeric")."<br>";
		
	if(sxREQUEST("attr_length") == "" )
		$_REQUEST["attr_length"] = "0";
	if(ereg("[^0-9_]",sxREQUEST("attr_length")))
		$errMsg .= _t("length not numeric")."<br>";
		
	if(sxREQUEST("attr_decimals") == "" )
		$_REQUEST["attr_decimals"] = "0";
	if(ereg("[^0-9_]",sxREQUEST("attr_decimals")))
		$errMsg .= _t("decimals not numeric")."<br>";
		
	
	if ((0+sxREQUEST("attr_decimals")) > (0+sxREQUEST("attr_length"))) {
		$errMsg .= _t("decimals greater than length")."<br>";
	}
	if($errMsg != ""){
		return $errMsg; 
	}
	$description = sxREQUEST("description");
	//check domain insert
	if(isset($_REQUEST["new_domain"]) && $_REQUEST["new_domain"] != "") {
		$_REQUEST["domain"] = $_REQUEST["dom_domain"];
		$_REQUEST["description"] = $_REQUEST["attribute"];
		$_REQUEST["length"] = $_REQUEST["dom_length"];
		$_REQUEST["decimal"] = $_REQUEST["dom_decimals"];
		$_REQUEST["lkp_entity"] = "";
		$_REQUEST["lkp_attribute"] = "";
		$_REQUEST["enumerable"] = "N";
		if($_REQUEST["new_domain"] == "NDL"){
			$_REQUEST["lkp_entity"] = $_REQUEST["entity"];
			$_REQUEST["lkp_attribute"] = $_REQUEST["attribute"];
			$_REQUEST["enumerable"] = "E";
		}
		$_REQUEST["frequent"] = "N";
		$_REQUEST["note"] = "";
		$errMsg .= domainIns("NOREDIR");
	}
	if($errMsg != ""){
		return $errMsg; 
	}
	//check length not defined in domain
	$sSQL = "SELECT d.* FROM domain d " .
				"	WHERE d.domain = '".sxRAS("domain")."'";
	$objSDAO->query($sSQL,"test");
	if ($row = $objSDAO->fetch("test")) {
		if (strpos("string,variable-string,number",$row['type'])===false) {
			if($row["length"] != 0)
				$errMsg .=_t("no length for type '%s'",$row['type'])."<br>";			
		} else {
			if (sxREQUEST("attr_length")== 0) {
				if($row["length"] == 0)
					$errMsg .=_t("length missing")."<br>";
			} else {
				if($row["length"] != 0)
					$errMsg .=_t("no length allowed")."<br>";
			}
		}
		if ($row['type']!='number') {
			if(sxREQUEST("attr_decimals") != 0)
				$errMsg .=_t("Type = '%s' - decimals allowed for type 'number' only",$row['type'])."<br>";
		}			
	} else {
		$errMsg .=_t("Domain not found")."<br>";
	}
	//check 1 pk autoincr
	if (sxREQUEST('primary_key')=='X') {
		$sSQL = "SELECT count(*) as num FROM attribute " .
					"	WHERE entity = '".sxRAS("entity").
					"' AND attribute <> '".sxRAS("attribute").
					"' AND primary_key <> 'N'";
		$numEntity = 0;
		$objSDAO->query($sSQL,"test");
		$row = $objSDAO->fetch("test");
		if($row["num"] != 0)
			$errMsg .=_t("primary-key autoincr not unique")."<br>";
	}
	if (sxREQUEST('primary_key')=='Y') {
		$sSQL = "SELECT count(*) as num FROM attribute " .
					"	WHERE entity = '".sxRAS("entity").
					"' AND attribute <> '".sxRAS("attribute").
					"' AND primary_key = 'X'";
		$numEntity = 0;
		$objSDAO->query($sSQL,"test");
		$row = $objSDAO->fetch("test");
		if($row["num"] != 0)
			$errMsg .=_t("primary-key autoincr not unique")."<br>";
	}
	//check existence
	$sSQL = "SELECT count(*) as num FROM attribute " .
				"	WHERE entity='".sxRAS("entity").
				"' AND attribute='".sxRAS("attribute")."'";
	$numEntity = 0;
	$objSDAO->query($sSQL,"test");
	$row = $objSDAO->fetch("test");
	if($row["num"] != 0)
		$errMsg .=_t("existing attribute")."<br>";

	if($errMsg != "") {
		return $errMsg;
	}
	
	if($errMsg != ""){
		return $errMsg;
	}
	$upgrade_status = sxREQUEST("upgrade_status");
	$index = (isset($_REQUEST["index"]))?"Y":"N";
	$primary_key = $_REQUEST["primary_key"];
	if (trim($description)=='') $description = sxREQUEST("attribute");
	$sSQL = "INSERT INTO " .
			"attribute " .
			"(entity, " .
			"attribute, " .
			"length, " .
			"decimals, " .
			"domain, " .
			"default_value, " .
			"upgrade_status, " .
			"indexed, " .
			"primary_key, " .
			"physical_param, " .
			"description, " .
			"sequence, " .
			"note)" .
			"VALUES('".sxRAS("entity")."', " .
			"'".sxRAS("attribute")."', " .
			sxRAS("attr_length").", " .
			sxRAS("attr_decimals").", " .
			"'".sxRAS("domain")."', " .
			"'".sxRAS("default")."', " .
			"'".$upgrade_status."', " .
			"'".$index."', " .
			"'".$primary_key."', " .
			"'".sxRAS("phys_param")."', " .
			"'".sxAS($description)."', " .
			sxRAS("sequence").", " .
			"'".sxRAS("note")."'" .
			")";	
	$objSDAO->query($sSQL,"ins");
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);		
	exit();
}

/**
 * aggiorna l'attribute
 */
function attributeUpd(){
	global $objSDAO;
	$errMsg = "";
	$_REQUEST["new_attribute"] = strtolower(trim($_REQUEST["new_attribute"]));
	if(ereg("[^a-z0-9_]",sxREQUEST("new_attribute")))
		$errMsg .= _t("name - a-z0-9 _ allowed").".<br>";
	if(sxREQUEST("new_attribute") == "")
		$errMsg .= _t("missing name")."<br>";
		
	if(sxREQUEST("sequence") == "" )
		$_REQUEST["sequence"] = "0";
	if(ereg("[^0-9_]",sxREQUEST("sequence")))
		$errMsg .= _t("sequence not numeric")."<br>";
		
	if(sxREQUEST("attr_length") == "" )
		$_REQUEST["attr_length"] = "0";
	if(ereg("[^0-9_]",sxREQUEST("attr_length")))
		$errMsg .= _t("length not numeric")."<br>";
		
	if(sxREQUEST("attr_decimals") == "" )
		$_REQUEST["attr_decimals"] = "0";
	if(ereg("[^0-9_]",sxREQUEST("attr_decimals")))
		$errMsg .= _t("decimals not numeric")."<br>";
	
	if ((0+sxREQUEST("attr_decimals")) > (0+sxREQUEST("attr_length"))) {
		$errMsg .= _t("decimals greater than length")."<br>";
	}
	if($errMsg != ""){return $errMsg;}
	//check domain existence
	$description = sxREQUEST('description');
	if(isset($_REQUEST["new_domain"]) && $_REQUEST["new_domain"] != ""){
		$_REQUEST["domain"] = $_REQUEST["dom_domain"];
		$_REQUEST["description"] = $_REQUEST["attribute"];
		$_REQUEST["length"] = $_REQUEST["dom_length"];
		$_REQUEST["decimal"] = $_REQUEST["dom_decimals"];
		$_REQUEST["enumerable"] = "N";
		$_REQUEST["lkp_entity"] = "";
		$_REQUEST["lkp_attribute"] = "";
		if($_REQUEST["new_domain"] == "NDL"){
			$_REQUEST["lkp_entity"] = $_REQUEST["entity"];
			$_REQUEST["lkp_attribute"] = $_REQUEST["attribute"];
			$_REQUEST["enumerable"] = "E";
		}
		$_REQUEST["note"] = "";
		$_REQUEST["frequent"] = "N";
		$errMsg .= domainIns("NOREDIR");
	}
	if($errMsg != ""){return $errMsg;}
	//check length not defined in domain
	$sSQL = "SELECT d.* FROM domain d " .
				"	WHERE d.domain = '".sxRAS("domain")."'";
	$objSDAO->query($sSQL,"test");
	if ($row = $objSDAO->fetch("test")) {
		if (strpos("string,variable-string,number",$row['type'])===false) {
			if($row["length"] != 0)
				$errMsg .=_t("no length for type '%s'",$row['type'])."<br>";			
		} else {
			if (sxREQUEST("attr_length")== 0) {
				if($row["length"] == 0)
					$errMsg .=_t("missing length")."<br>";
			} else {
				if($row["length"] != 0)
					$errMsg .=_t("no length allowed")."<br>";
			}
		}
		if ($row['type']!='number') {
			if(sxREQUEST("attr_decimals") != 0)
				$errMsg .=_t("type = '%s' - decimals allowed for type 'number' only",$row['type'])."<br>";
		}			
	} else {
		$errMsg .=_t("domain not found")."<br>";
	}
	//check 1 pk autoincrement
	if (sxREQUEST('primary_key')=='X') {
		$sSQL = "SELECT count(*) as num FROM attribute " .
					"	WHERE entity = '".sxRAS("entity").
					"' AND attribute <> '".sxRAS("attribute").
					"' AND primary_key <> 'N'";
		$numEntity = 0;
		$objSDAO->query($sSQL,"test");
		$row = $objSDAO->fetch("test");
		if($row["num"] != 0)
			$errMsg .=_t("primary-key autoincr not unique")."<br>";
	}
	//check 1 pk autoincrement
	if (sxREQUEST('primary_key')=='Y') {
		$sSQL = "SELECT count(*) as num FROM attribute " .
					"	WHERE entity = '".sxRAS("entity").
					"' AND attribute <> '".sxRAS("attribute").
					"' AND primary_key = 'X'";
		$numEntity = 0;
		$objSDAO->query($sSQL,"test");
		$row = $objSDAO->fetch("test");
		if($row["num"] != 0)
			$errMsg .=_t("primary-key autoincr not unique")."<br>";
	}
	
	if($errMsg != ""){return $errMsg;}
	
	
	//check existence
	if ($_REQUEST["new_attribute"] != $_REQUEST["attribute"]) {
		$sSQL = "SELECT count(*) as num FROM attribute " .
				" WHERE entity = '".$_REQUEST["entity"]."' " .
				" AND attribute = '".$_REQUEST["new_attribute"]."'";
		$objSDAO->query($sSQL,"test");
		$row = $objSDAO->fetch("test");
		if($row["num"] != 0)
			$errMsg .= _t("existing atttribute");
	}
	
	if($errMsg != "")
		return $errMsg;
	
	$upgrade_status = sxREQUEST("upgrade_status");
	$index = (isset($_REQUEST["index"]))?"Y":"N";
	$primary_key = $_REQUEST["primary_key"];
	$length = ($_REQUEST["attr_length"] == "")?0:$_REQUEST["attr_length"];
	$decimals = ($_REQUEST["attr_decimals"] == "")?0:$_REQUEST["attr_decimals"];
	$sequence = ($_REQUEST["sequence"] == "")?0:$_REQUEST["sequence"];
	$sSQL = "UPDATE " .
			"attribute SET length=".$length.", " .
			"decimals=".$decimals.", " .
			"sequence=".$sequence.", " .
			"domain='".sxRAS("domain")."', " .
			"default_value='".sxRAS("default")."', " .
			"upgrade_status='".$upgrade_status."', " .
			"indexed='".$index."', " .
			"primary_key='".$primary_key."', " .
			"physical_param='".sxRAS("phys_param")."', " .
			"description='".sxAS($description)."', " .
			"note='".sxRAS("note")."' " .
			"WHERE attribute='".sxRAS("attribute")."' AND entity='".sxRAS("entity")."'";	
	$objSDAO->exec($sSQL);
	if ($_REQUEST["new_attribute"] != $_REQUEST["attribute"]) {
		$sSQL = "UPDATE attribute SET attribute = '".$_REQUEST["new_attribute"]."'" .
			" WHERE attribute = '".$_REQUEST["attribute"]."' AND entity = '".$_REQUEST["entity"]."'";	
		$objSDAO->query($sSQL,"upd");
		$sSQL = "UPDATE relationship_key SET parent_attribute = '".$_REQUEST["new_attribute"]."'" .
			" WHERE parent_attribute = '".$_REQUEST["attribute"]."' AND parent_entity = '".$_REQUEST["entity"]."'";	
		$objSDAO->query($sSQL,"upd");
		$sSQL = "UPDATE relationship_key SET child_attribute = '".$_REQUEST["new_attribute"]."'" .
			" WHERE child_attribute = '".$_REQUEST["attribute"]."' AND child_entity = '".$_REQUEST["entity"]."'";	
		$objSDAO->query($sSQL,"upd");
		$sSQL = "UPDATE domain SET lookup_attribute = '".$_REQUEST["new_attribute"]."'" .
			" WHERE lookup_attribute = '".$_REQUEST["attribute"]."' AND lookup_entity = '".$_REQUEST["entity"]."'";	
		$objSDAO->query($sSQL,"upd");
		$sSQL = "UPDATE subtyping SET discriminating_attribute = '".$_REQUEST["new_attribute"]."'" .
			" WHERE discriminating_attribute = '".$_REQUEST["attribute"]."' AND supertype_entity = '".$_REQUEST["entity"]."'";	
		$objSDAO->query($sSQL,"upd");
	}
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	exit();
}

/**
 * elimina l'attribute
 */
function attributeDel(){
	global $objSDAO;
	$errMsg = "";
	if(!isset($_REQUEST["entity"]) || $_REQUEST["entity"] == "")
		$errMsg .= _t("missing entity")."<br>";
	if(!isset($_REQUEST["attribute"]) || $_REQUEST["attribute"] == "")
		$errMsg .= _t("missing attribute")."<br>";
	if($errMsg == ""){
		$sSQL = "DELETE FROM attribute " .
				"WHERE attribute='".$_REQUEST["attribute"]."' AND entity='".$_REQUEST["entity"]."'";	
		if($objSDAO->query($sSQL,"ins",array("contOnErr"=>true))){
			logRecord("520", _t("Eliminato l'attribute")." '".addslashes(sxGPC(strtolower($_REQUEST["attribute"]))."' dall'entity '".addslashes(sxGPC(strtolower($_REQUEST["entity"])))."'"));
			//serverRedir("attributeList.php?da=".$_REQUEST["data_area"]."&ent=".$_REQUEST["entity"]."&xsn=".XSN);
			$url = $_SESSION[USEROBJ]->getHistory(-1, true);
			serverRedir($url);
		}else{
			$errMsg .= $sSQL."<br> - ".$objSDAO->error;
		}
	}
	return $errMsg;
}
